package org.jypj.dev.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    // 取次日凌晨00:00:00
    @SuppressWarnings("static-access")
    public static Date weeHours(Date date, int flag, int offset) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(cal.DATE, offset); // 加offset天
        int hour = cal.get(Calendar.HOUR_OF_DAY);
        int minute = cal.get(Calendar.MINUTE);
        int second = cal.get(Calendar.SECOND);
        // 时分秒（毫秒数）
        long millisecond = hour * 60 * 60 * 1000 + minute * 60 * 1000 + second * 1000;
        // 凌晨00:00:00
        cal.setTimeInMillis(cal.getTimeInMillis() - millisecond);
        if (flag == 0) {
            return cal.getTime();
        } else if (flag == 1) {
            // 凌晨23:59:59
            cal.setTimeInMillis(cal.getTimeInMillis() + 23 * 60 * 60 * 1000 + 59 * 60 * 1000 + 59 * 1000);
        }
        return cal.getTime();
    }

    // 取时间差(单位:分)
    public static int getDateOffset(String endDate, String firstDate) {
        int mins = 0;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long endTime = sdf.parse(endDate).getTime();
            long beforeTime = sdf.parse(firstDate).getTime();
            long diff = endTime - beforeTime;
            mins = (int) (diff / (1000 * 60));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mins;
    }

    /**
     * 得到今天的日期
     *
     * @param pattern
     * @return
     */
    public static String getTodayString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        String format = sdf.format(today);
        return format;
    }

    /**
     * 得到今天的特殊日期
     *
     * @param pattern
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getTodayString2(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        if (today.getHours() < 2) {
            long time = today.getTime() - 24 * 60 * 60 * 1000;
            today.setTime(time);
        }
        String format = sdf.format(today);
        return format;
    }

    /**
     * 得到明天的日期
     *
     * @param pattern
     * @return
     */
    public static String getTomorrowString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        // 通过秒获取下一天日期
        long time = (today.getTime() / 1000) + 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到某一天的前几天或者后几天
     *
     * @param pattern
     * @param days    正数表示后几天，负数表示后几天
     * @return
     */
    public static String getSomeDayString(String dateStr, String pattern, int days) {
        String tomorrow = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date date = sdf.parse(dateStr);
            // 通过秒获取下一天日期
            long time = (date.getTime() / 1000) + days * 60 * 60 * 24;// 秒
            date.setTime(time * 1000);// 毫秒
            tomorrow = sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tomorrow;
    }

    /**
     * 得到昨天的的日期
     *
     * @param pattern
     * @return
     */
    public static String getYestodayString(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        Date today = new Date();
        // 通过秒获取前一天的日期
        long time = (today.getTime() / 1000) - 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到昨天的的日期
     *
     * @param pattern
     * @return
     */
    @SuppressWarnings("deprecation")
    public static String getYestodayString2(String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        int day = 1;
        Date today = new Date();
        int hour = today.getHours();
        if (hour < 2) {
            day = 2;
        }
        System.out.println(hour);
        // 通过秒获取前一天的日期
        long time = (today.getTime() / 1000) - day * 60 * 60 * 24;// 秒
        today.setTime(time * 1000);// 毫秒
        String tomorrow = sdf.format(today);
        return tomorrow;
    }

    /**
     * 得到某一天的第二天
     *
     * @param pattern
     * @return
     */
    public static String getTomorrowStringForSomeDay(String pattern, String day) {
        String tomorrow = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            Date today = sdf.parse(day);
            // 通过秒获取下一天日期
            long time = (today.getTime() / 1000) + 60 * 60 * 24;// 秒
            today.setTime(time * 1000);// 毫秒
            tomorrow = sdf.format(today);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return tomorrow;
    }

    // 比较日期大小
    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            // date1 > date2
            return 1;
        } else if (date1.getTime() < date2.getTime()) {
            // date1 < date2
            return -1;
        } else {
            // date1 == date2
            return 0;
        }
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param date         当前时间 yyyy-MM-dd HH:mm:ss
     * @param startTime 开始时间 00:00:00
     * @param endTime   结束时间 00:05:00
     * @return
     */
    public static boolean isInDate(Date date, String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
        String currentTime = format.substring(11, format.length());
        if (currentTime.compareTo(startTime) >= 0 && currentTime.compareTo(endTime) <= 0) {
            return true;
        }
        return false;
    }

    /**
     * 从时间得到秒数 0-2:00:00特殊计算如果
     *
     * @param time
     * @return
     */
    public static int getSecondFromTime(String time) {
        String[] split = time.split(":");
        int second = Integer.parseInt(split[0]) * 3600 + Integer.parseInt(split[1]) * 60 + Integer.parseInt(split[2]);
        if (second < 7200) {
            second += 24 * 60 * 60;
        }
        return second;
    }

    /**
     * 得到几天前零点的时间戳 负数表示几天后的
     *
     * @param days
     * @return
     */
    public static String get0StrTimeForDays(int days, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        long currentTime = new Date().getTime();
        long fourDaysBeforeTime = currentTime - (currentTime % (24 * 60 * 60 * 1000)) - days * 24 * 60 * 60 * 1000
                - 8 * 60 * 60 * 1000;
        String format = sdf.format(new Date(fourDaysBeforeTime));
        return format;
    }

    /**
     * 得到几天前零点的毫秒值 负数表示几天后的
     *
     * @param days
     * @return
     */
    public static long get0LongTimeForDays(int days, String pattern) {
        long currentTime = new Date().getTime();
        long fourDaysBeforeTime = currentTime - (currentTime % (24 * 60 * 60 * 1000)) - days * 24 * 60 * 60 * 1000
                - 8 * 60 * 60 * 1000;
        return fourDaysBeforeTime;
    }

    public static void main(String[] args) {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime=sdf.format(new Date());
        System.out.println(currentTime.substring(0,10));
        System.out.println(currentTime.substring(11,16));
    }

}
